package com.zhu.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhu.edu.entity.ChooseCourse;
import com.zhu.edu.entity.Permission;
import com.zhu.edu.entity.User;
import com.zhu.edu.mapper.ChooseCourseMapper;
import com.zhu.edu.mapper.PermissionMapper;
import com.zhu.edu.mapper.UserMapper;
import com.zhu.edu.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zjc
 * @since 2021-12-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private ChooseCourseMapper chooseCourseMapper;

    @Override
    public User getUserByUsername(String username) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public List<Map<String, Object>> getMenuList(String username) {
        List<Map<String, Object>> list = new ArrayList<>();
        //父权限
        List<Permission> permissionList = permissionMapper.selectFatherPermissionByUsername(username);
        for (Permission permission : permissionList) {
            Map<String, Object> map = new HashMap<>();
            map.put("menu", permission);
            //子权限
            List<Permission> permissionList1 = permissionMapper.selectFatherPermissionByFatherId(permission.getPermissionId());
            map.put("subMenu",permissionList1);
            list.add(map);
        }
        return list;
    }

    @Override
    public List<User> getStudentListByCourseId(Integer courseId) {

        return userMapper.getStudentListByCourseId(courseId);
    }

    @Override
    public List<Map<String, Object>> getStudentsGradeByCourseId(Integer courseId) {
        List<Map<String, Object>> maps = new ArrayList<>();
        List<User> users = userMapper.getStudentListByCourseId(courseId);
        for (User user : users) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", user.getUserId());
            queryWrapper.eq("course_id", courseId);
            ChooseCourse chooseCourse = chooseCourseMapper.selectOne(queryWrapper);
            Map<String, Object> map = new HashMap<>();
            map.put("student", user);
            map.put("grade", chooseCourse);
            maps.add(map);
        }
        return maps;
    }

    @Override
    public List<User> getAllTeachersBySchoolId(Integer schoolId) {
        return userMapper.getAllTeachersBySchoolId(schoolId);
    }
}
